namespace Kopernikus.Parsing.GenericActions
{
    public class SemanticActionParametersFixup : ISemanticActionParametersFixup
    {
        private readonly int[] m_parameterFromRhsSymbolMapping;

        public SemanticActionParametersFixup(int[] parameterFromRhsSymbolMapping)
        {
            m_parameterFromRhsSymbolMapping = parameterFromRhsSymbolMapping;
        }

        #region ISemanticActionParametersFixup Members

        public bool HasFixups
        {
            get { return m_parameterFromRhsSymbolMapping != null; }
        }

        public object[] FixupParameters(object[] parameters)
        {
            if (m_parameterFromRhsSymbolMapping == null)
            {
                return parameters;
            }
            object[] fixup = new object[m_parameterFromRhsSymbolMapping.Length];
            for (int i = 0; i < m_parameterFromRhsSymbolMapping.Length; ++i)
            {
                fixup[i] = parameters[m_parameterFromRhsSymbolMapping[i]];
            }
            return fixup;
        }

        public T[] Fixup<T>(T[] a)
        {
            if (m_parameterFromRhsSymbolMapping == null)
            {
                return a;
            }
            T[] fixup = new T[m_parameterFromRhsSymbolMapping.Length];
            for (int i = 0; i < m_parameterFromRhsSymbolMapping.Length; ++i)
            {
                fixup[i] = a[m_parameterFromRhsSymbolMapping[i]];
            }
            return fixup;
        }

        #endregion
    }
}